package com.yanqu.road.server.cmd.unionwar;

import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarSimple;
import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUser;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.unionwar.CrossUnionWarProto;
import com.yanqu.road.pb.union.UnionActivityGroupProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C_UNION_WAR_IS_COMMANDER, desc = "是否是指挥官")
public class CrossUnionWarIsCommanderCmdCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        long pid = packet.getPlayerId();
        UnionActivityGroupProto.BaseUnionActivityGroupMsg baseMsg = UnionActivityGroupProto.BaseUnionActivityGroupMsg.parseFrom(packet.getBytes());
        packet = YanQuMessageUtils.buildMessageByte(baseMsg.getCode(), baseMsg.getMsg().toByteArray());
        packet.setPlayerId(pid);

        CrossUnionWarProto.CrossUnionWarIsCommanderReqMsg reqMsg = CrossUnionWarProto.CrossUnionWarIsCommanderReqMsg.parseFrom(packet.getBytes());

        CrossUnionWarProto.CrossUnionWarIsCommanderRespMsg.Builder clientMsg = CrossUnionWarProto.CrossUnionWarIsCommanderRespMsg.newBuilder();
        clientMsg.setRet(0);
        clientMsg.setIsCommander(false);
        clientMsg.setCommander(0);

        UnionActivityGroupUnion unionData = Cross2UnionActivityGroupMgr.getUnionData(reqMsg.getActivityId(), packet.getPlayerId());
        if(unionData != null){
            CrossUnionWarSimple unionWarSimple = CrossUnionWarMgr.getCrossUnionWarSimple(reqMsg.getActivityId(), unionData.getUnionUid());
            if(unionWarSimple != null){
                clientMsg.setIsCommander(unionWarSimple.getCommander() == packet.getPlayerId());
                clientMsg.setCommander(unionWarSimple.getCommander());

                if(unionWarSimple.getCommander() > 0){
                    UnionActivityGroupUser groupUser = unionData.getUserMap().get(unionWarSimple.getCommander());
                    if(groupUser != null && groupUser.getPosition() == eUnionPosition.Master.getValue()){
                        //指挥官也是会长，然后去掉指挥官
                        clientMsg.setCommander(0);
                        unionWarSimple.setCommander(0);
                    }
                }
            }
            UnionActivityGroupUser groupUser = unionData.getUserMap().get(packet.getPlayerId());
            if(groupUser != null){
                if(groupUser.getPosition() == eUnionPosition.Master.getValue()){
                    clientMsg.setIsCommander(true);
                }
            }
        }

        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_UNION_WAR_IS_COMMANDER, clientMsg));
    }

    @Override
    public void sendData() {

    }
}
